package indi.yuluo.service.impl;

import indi.yuluo.dao.CodeMapper;
import indi.yuluo.entity.problem.Language;
import indi.yuluo.service.CodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: yuluo
 * @CreateTime: 2022-08-17  09:59
 * @Description: TODO
 */

@Slf4j
@Service
public class CodeServiceImpl implements CodeService {

    @Resource
    private CodeMapper codeMapper;


    /**
     * 获取code模板信息
     * @return
     */
    @Override
    public Map<String, String> getCodeTemplates() {

        HashMap<String, String> resMap = new HashMap<>();

        List<Language> list = codeMapper.getLanguageInfo();

        ArrayList<Long> ids = new ArrayList<>();
        for (int i = 0; i < 10; i ++) {
            ids.add((long) i);
        }

        // 收集content-type字段
        List<String> collect = list.stream()
                .filter(language -> ids.contains(language.getId()))
                .map(Language::getName)
                .collect(Collectors.toList());

        // 收集templates字段
        List<String> templates = list.stream()
                .filter(language -> ids.contains(language.getId()))
                .map(Language::getTemplate)
                .collect(Collectors.toList());

        // 将content-type和templates赋值给map集合
        for (int i = 0; i < 9; i++) {
            resMap.put(collect.get(i), templates.get(i));
        }

        // 输出map查看结果
        resMap.forEach((key, value) -> {
            System.out.println(key + " : " + value);
        });

        return resMap;
    }

    /**
     * 获取语言信息
     * @return
     */
    @Override
    public List<String> getCodeType() {

        List<Language> languageInfo = codeMapper.getLanguageInfo();

        // 使用stream流过滤数据信息
        List<String> collect = languageInfo.stream()
                .map(Language::getName)
                .collect(Collectors.toList());

        return collect;
    }
}
